<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!--<el-form-item label="客户编号" prop="customCode">
        <el-input
          v-model="queryParams.customCode"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="会员昵称" prop="customName">
        <el-input
          v-model="queryParams.customName"
          placeholder="请输入会员昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员手机号" prop="customPhone">
        <el-input
          v-model="queryParams.customPhone"
          placeholder="请输入会员手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户属性" prop="customType">
        <el-select v-model="queryParams.customType" placeholder="请选择用户属性" clearable>
          <el-option v-for="dict in dict.type.custom_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="客户邮箱" prop="customEmail">
              <el-input
                v-model="queryParams.customEmail"
                placeholder="请输入客户邮箱"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="充值金额" prop="customRechargeBalance">
              <el-input
                v-model="queryParams.customRechargeBalance"
                placeholder="请输入充值金额"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="赠送金额" prop="customGiveBalance">
              <el-input
                v-model="queryParams.customGiveBalance"
                placeholder="请输入赠送金额"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="冻结金额" prop="balanceFreezeBalance">
              <el-input
                v-model="queryParams.balanceFreezeBalance"
                placeholder="请输入冻结金额"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['custom:user:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['custom:user:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['custom:user:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['custom:user:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="resetQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!-- <el-table-column label="客户id" align="center" prop="customId" />-->
      <!--      <el-table-column label="会员头像" align="center" prop="customPhoto" width="100">
              <template slot-scope="scope">
                <image-preview :src="scope.row.customPhoto" :width="50" :height="50"/>
              </template>
            </el-table-column>-->
      <!--<el-table-column label="会员编号" align="center" prop="customCode" />-->
      <el-table-column label="会员昵称" align="center" prop="customName"/>
      <el-table-column label="会员手机号" align="center" prop="customPhone"/>
      <el-table-column label="用户属性" align="center" prop="customType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.custom_type" :value="scope.row.customType"/>
        </template>
      </el-table-column>
      <el-table-column label="充电次数" align="center" prop="chargingCycles"/>
      <el-table-column label="注册时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆信息" align="center" prop="carName">
        <template slot-scope="scope">
          <span>{{scope.row.carName}}/{{scope.row.customCarNumber}}</span>
        </template>
      </el-table-column>

      <!--<el-table-column label="客户车牌号" align="center" prop="customCarNumber" />
      <el-table-column label="客户邮箱" align="center" prop="customEmail" />
      <el-table-column label="充值金额" align="center" prop="customRechargeBalance" />
      <el-table-column label="赠送金额" align="center" prop="customGiveBalance" />
      <el-table-column label="冻结金额" align="center" prop="balanceFreezeBalance" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="viewCustomOrderInfo(scope.row)"
            v-hasPermi="['custom:orderInfo:list','custom:orderInfo:query']"
          >订单
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['custom:user:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['custom:user:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="会员编号" prop="customCode">
          <el-input v-model="form.customCode" placeholder="请输入客户编号" readonly/>
        </el-form-item>
        <el-form-item label="会员名字" prop="customName">
          <el-input v-model="form.customName" placeholder="请输入客户名字" readonly/>
        </el-form-item>
        <el-form-item label="用户属性" prop="customType">
          <el-select v-model="form.customType" placeholder="请选择用户属性" style="width: 100%">
            <el-option v-for="dict in dict.type.custom_type" :key="dict.value" :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员手机号" prop="customPhone">
          <el-input v-model="form.customPhone" placeholder="请输入客户手机号" readonly/>
        </el-form-item>
        <el-form-item label="会员邮箱" prop="customEmail">
          <el-input v-model="form.customEmail" placeholder="请输入客户邮箱" readonly/>
        </el-form-item>
        <el-form-item label="会员头像">
          <div style="width: 100px;height: 100px">
            <img style="width: 100%;height: 100%;" :src="form.customPhoto">
          </div>
          <!--          <image-upload v-model="form.customPhoto" v-else/>-->
        </el-form-item>
        <el-form-item label="充值金额" prop="customRechargeBalance">
          <el-input v-model="form.customRechargeBalance" placeholder="请输入充值金额"/>
        </el-form-item>
        <el-form-item label="赠送金额" prop="customGiveBalance">
          <el-input v-model="form.customGiveBalance" placeholder="请输入赠送金额"/>
        </el-form-item>
        <el-form-item label="冻结金额" prop="balanceFreezeBalance">
          <el-input v-model="form.balanceFreezeBalance" placeholder="请输入冻结金额"/>
        </el-form-item>
        <el-form-item label="汽车型号" prop="customCarType">
          <el-cascader filterable style="width: 100%;" :props="{value:'id'}" placeholder="请选择汽车型号"
                       :options="carTreeList" :show-all-levels="false" v-model="form.customCarType"></el-cascader>
        </el-form-item>
        <el-form-item label="车牌号" prop="customCarNumber">
          <el-input v-model="form.customCarNumber" placeholder="请输入车牌号" readonly/>
        </el-form-item>
        <el-form-item label="汽车生产年月" prop="customCarProduceTime" style="width: 100%;">
          <el-date-picker clearable
                          v-model="form.customCarProduceTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="汽车生产年月" readonly>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="续航" prop="customCarEndurance">
          <el-input v-model="form.customCarEndurance" placeholder="请输入续航里程" readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listUser, getUser, delUser, addUser, updateUser } from '@/api/smart/custom/user'
  import { listTree } from '@/api/smart/operator/car'

  export default {
    name: 'User',
    dicts: ['custom_type'],
    data() {
      return {
        reqFileUrl: 'https://www.sdzctech.cn',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户表格数据
        userList: [],
        // 汽车库表格数据
        carTreeList: null,
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customCode: null,
          customName: null,
          customType: null,
          customPhone: null,
          customEmail: null,
          customPhoto: null,
          customRechargeBalance: null,
          customGiveBalance: null,
          balanceFreezeBalance: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
      this.getCarTreeList()
    },
    methods: {
      /** 查询客户列表 */
      getList() {
        this.loading = true
        listUser(this.queryParams).then(response => {
          this.userList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 查询汽车库列表 */
      getCarTreeList() {
        listTree().then(response => {
          this.carTreeList = response.data
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          customId: null,
          customCode: null,
          customName: null,
          customType: null,
          customPhone: null,
          customEmail: null,
          customPhoto: null,
          customRechargeBalance: null,
          customGiveBalance: null,
          balanceFreezeBalance: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          customCarType: null,
          customCarNumber: null,
          customCarProduceTime: null,
          customCarEndurance: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.customId)
        this.names = selection.map(item => item.customName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.getCarTreeList()
        this.open = true
        this.title = '添加客户'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        let that = this
        this.reset()
        this.getCarTreeList()
        const customId = row.customId || this.ids
        getUser(customId).then(response => {
          if (response.data.customPhoto != null && response.data.customPhoto != undefined && response.data.customPhoto != ''
            && response.data.customPhoto.indexOf('https://www.sdzctech.cn/') == -1 && response.data.customPhoto.indexOf('https://thirdwx.qlogo.cn') == -1) {
            response.data.customPhoto = that.reqFileUrl + response.data.customPhoto
          }
          this.form = response.data
          this.open = true
          this.title = '修改客户'
        })
      },
      /** 提交按钮 */
      submitForm() {
        console.log(this.form.customCarType.length)
        if (this.form.customCarType.length == 2) {
          this.form.customCarType = this.form.customCarType[this.form.customCarType.length - 1]
        }
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.customId != null) {
              updateUser(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addUser(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const customIds = row.customId || this.ids
        const customNames = row.customName || this.names
        this.$modal.confirm('是否确认删除会员昵称为"' + customNames + '"的数据项？').then(function() {
          return delUser(customIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('custom/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      },
      /*用户订单详情*/
      viewCustomOrderInfo(row) {
        this.$router.push({
          name: 'viewCustomOrderInfo',
          query: {
            customName: row.customName,
            customCode: row.customCode
          }
        })
      }
    }
  }
</script>
